package com.kun.base.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.animation.Animation
import android.view.animation.CycleInterpolator
import android.view.animation.TranslateAnimation
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatEditText
import com.kun.base.R

class ClearEditText @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyle: Int = android.R.attr.editTextStyle
) : AppCompatEditText(context!!, attrs, defStyle), OnFocusChangeListener, TextWatcher {
    private var mClearDrawable: Drawable? = null
    private var hasFoucs = false
    private fun init() {
        mClearDrawable = compoundDrawables[2]
        if (mClearDrawable == null) {
            mClearDrawable = resources.getDrawable(R.mipmap.import_icon_delete)
        }
        mClearDrawable!!.setBounds(
            0, 0, mClearDrawable!!.getIntrinsicWidth(),
            mClearDrawable!!.getIntrinsicHeight()
        )
        setClearIconVisible(false)
        onFocusChangeListener = this
        addTextChangedListener(this)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            if (compoundDrawables[2] != null) {
                val touchable =
                    (event.x > width - totalPaddingRight
                            && event.x < width - paddingRight)
                if (touchable) {
                    this.setText("")
                    if (callback != null) {
                        callback!!.clear()
                    }
                }
            }
        }
        return super.onTouchEvent(event)
    }

    private var callback: ClearTextCallback? = null

    /**
     * 文字清空后的回调
     */
    interface ClearTextCallback {
        fun clear()
    }

    fun clearOnListener(callback: ClearTextCallback?) {
        this.callback = callback
    }

    override fun onFocusChange(v: View, hasFocus: Boolean) {
        hasFoucs = hasFocus
        if (hasFocus) {
            setClearIconVisible(text!!.length > 0)
        } else {
            setClearIconVisible(false)
        }
    }

    fun setClearIconVisible(visible: Boolean) {
        val right = if (visible) mClearDrawable else null
        setCompoundDrawables(
            compoundDrawables[0], compoundDrawables[1],
            right, compoundDrawables[3]
        )
    }

    override fun onTextChanged(
        s: CharSequence,
        start: Int,
        count: Int,
        after: Int
    ) {
        if (hasFoucs) {
            setClearIconVisible(s.isNotEmpty())
        }
    }

    override fun beforeTextChanged(
        s: CharSequence,
        start: Int,
        count: Int,
        after: Int
    ) {
    }

    override fun afterTextChanged(s: Editable) {
        if (s.toString().isEmpty() && callback != null) {
            callback!!.clear()
        }
    }

    fun setShakeAnimation() {
        this.animation = shakeAnimation(5)
    }

    companion object {
        fun shakeAnimation(counts: Int): Animation {
            val translateAnimation: Animation = TranslateAnimation(0F, 10F, 0F, 0F)
            translateAnimation.interpolator = CycleInterpolator(counts.toFloat())
            translateAnimation.duration = 1000
            return translateAnimation
        }
    }

    init {
        init()
    }
}